﻿<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<link rel="stylesheet" type="text/css" href="../themes/default/easyui.css">
	<link rel="stylesheet" type="text/css" href="../themes/icon.css">
	<link rel="stylesheet" type="text/css" href="../demo.css">
	<script type="text/javascript" src="../jquery.min.js"></script>
	<script type="text/javascript" src="../jquery.easyui.min.js"></script>
    <script type="text/javascript" src="../extend.js"></script>
    <script>
        var dataListCache = null;
        var dataCache = null;
        var appId = GetQueryString("moduleid");
        var modules = null;
        var cmds = null;
        var editing = false;
        var rowEditing = -1;
        window.onresize=calSize;
        $(document).ready(function(){
            initModuleList();
            initCmdList();
            setData();
            initSiteList();
            initUserList();
            calSize();
		});
        function setData(){
            $.get("../../api/role/list",function(data){
                if(data.Result==0){
                    dataListCache = JSON.parse(data.Data);
                    $('#roleList').datagrid('loadData', dataListCache); 
                }
            },"json")
            .error(function(){
                $.messager.alert("错误","系统内部错误","error");
            })
            .complete(function() { 
            });
        }
        function calSize(){
            var height=window.parent.document.getElementById("mainFrame").clientHeight;
            var width=window.parent.document.getElementById("mainFrame").clientWidth;
            window.parent.document.getElementById(appId).height = height-35;
            $("#roleList").datagrid({height:height-80,width:width-20});
            $("#toolbar").panel({width:width-20});
        }
        function initVal(){
            $('#AutoId').val(dataCache.AutoId);
            $('#Flag').val(dataCache.Flag);
            $('#Flag').val(dataCache.Flag);
            $('#Code').textbox("setValue",dataCache.Code);
            $('#Name').textbox("setValue",dataCache.Name);
            $('#Note').textbox("setValue",dataCache.Note);
            listSiteForRole(dataCache.AutoId);
            listUserForRole(dataCache.AutoId);
        }
        function setUserForRole(){
            var userSelected = $('#UserSelected').combobox("getValues");
            var users = new Array();
            for(var i =0;i<userSelected.length;i++){
                users.push({AutoId:0,Flag:0,UserId:userSelected[i],RoleId:dataCache.AutoId});
            }
            
            $.post("../../api/role/setuser",{MasterId:dataCache.AutoId,Data:users},function(response){
                if(response.Result==0){
                    dataCache.User = JSON.parse(response.Data);
                }else{
                    $.messager.alert("错误",response.Message,"error");
                }
            })
            .error(function(){
                $.messager.alert("错误","系统内部错误","error");
            })
            .complete(function() { 
            });
        }
        function setSiteForRole(){
            var siteSelected = $('#SiteSelected').combobox("getValues");
            var sites = new Array();
            for(var i =0;i<siteSelected.length;i++){
                sites.push({AutoId:0,Flag:0,SiteId:siteSelected[i],RoleId:dataCache.AutoId});
            }
            
            $.post("../../api/role/setsite",{MasterId:dataCache.AutoId,Data:sites},function(response){
                if(response.Result==0){
                    dataCache.User = JSON.parse(response.Data);
                }else{
                    $.messager.alert("错误",response.Message,"error");
                }
            })
            .error(function(){
                $.messager.alert("错误","系统内部错误","error");
            })
            .complete(function() { 
            });
        }
        function listSiteForRole(roleId){
            if(roleId==0){
                $('#SiteSelected').combobox("clear");
                return ;
            }
            $.get("../../api/role/listsites?roleId="+roleId,function(data){
                if(data.Result==0){
                    var sites = JSON.parse(data.Data);
                    var siteId = new Array();
                    for(var i=0;i<sites.length;i++){
                        siteId.push(sites[i].SiteId);
                    }
                    if(dataCache.AutoId == roleId){
                        dataCache.Site = sites;
                    }
                    $('#SiteSelected').combobox("setValues",siteId);
                }
            },"json")
            .error(function(){
                $.messager.alert("错误","系统内部错误","error");
            })
            .complete(function() { 
            });
        }
        function listModuleForRole(roleId){
            if(roleId==0){
                $('#roleModuleList').datagrid("loadData",null);
                return ;
            }
            $.get("../../api/role/listmodules?roleId="+roleId,function(data){
                if(data.Result==0){
                    var modulelist = JSON.parse(data.Data);
                    for(var i =0;i<modulelist.length;i++){
                        if(modulelist[i].Exe){
                            modulelist[i].Exe = "true"
                        }else{
                            modulelist[i].Exe = "false"
                        }
                    }
                    $('#roleModuleList').datagrid("loadData",modulelist);
                }
            },"json")
            .error(function(){
                $.messager.alert("错误","系统内部错误","error");
            })
            .complete(function() { 
            });
        }
        function listUserForRole(roleId){
            if(roleId==0){
                $('#UserSelected').combobox("clear");
                return ;
            }
            $.get("../../api/role/listusers?roleId="+roleId,function(data){
                if(data.Result==0){
                    var users = JSON.parse(data.Data);
                    var userId = new Array();
                    for(var i=0;i<users.length;i++){
                        userId.push(users[i].UserId);
                    }
                    if(dataCache.AutoId == roleId){
                        dataCache.User = users;
                    }
                    $('#UserSelected').combobox("setValues",userId);
                }
            },"json")
            .error(function(){
                $.messager.alert("错误","系统内部错误","error");
            })
            .complete(function() { 
            });
        }
        function initSiteList(){
            $.get("../../api/site/list",function(data){
                if(data.Result==0){
                    var sites = JSON.parse(data.Data);
                    $('#SiteSelected').combobox('loadData', sites); 
                }
            },"json")
            .error(function(){
                $.messager.alert("错误","系统内部错误","error");
            })
            .complete(function() { 
            });
        }
        function initCmdList(){
            $.get("../../api/rolecmd/list",function(data){
                if(data.Result==0){
                    cmds = JSON.parse(data.Data);
                }
            },"json")
            .error(function(){
                $.messager.alert("错误","系统内部错误","error");
            })
            .complete(function() { 
            });
        }
        function initModuleList(){
            $.get("../../api/modules/list",function(data){
                if(data.Result==0){
                    modules = JSON.parse(data.Data);
                }
            },"json")
            .error(function(){
                $.messager.alert("错误","系统内部错误","error");
            })
            .complete(function() { 
            });
        }
        function initUserList(){
            $.get("../../api/user/list",function(data){
                if(data.Result==0){
                    var sites = JSON.parse(data.Data);
                    $('#UserSelected').combobox('loadData', sites); 
                }
            },"json")
            .error(function(){
                $.messager.alert("错误","系统内部错误","error");
            })
            .complete(function() { 
            });
        }
        function createNewRecord(){
            dataCache = {AutoId:0,Flag:0,Code:'',Name:'',Note:''};
            initVal();
            $('#editContent').window('open');
        }
        function editRecord(rowIndex,rowData){
            dataCache = rowData;
            initVal();
            $('#editContent').window('open');
        }
        function loadDataCache(rowIndex,rowData){
            dataCache = rowData;
        }
        function submitChange(){
            if (dataCache == null) {
                $.messager.alert("错误", "无法获取被编辑的对象", "error");
                return;
            }
            showMask();
            dataCache.Flag = $("#Flag").val();
            dataCache.Code=$("#Code").textbox("getValue");
            dataCache.Name=$("#Name").textbox("getValue");
            dataCache.Note=$("#Note").textbox("getValue");
            $.post("../../api/role/set",dataCache,function(response){
                if(response.Result==0){
                    dataCache = JSON.parse(response.Data);
                    setUserForRole();
                    setSiteForRole();
                    if(dataCache.Flag!=1){
                        for(var i=0;i<dataListCache.length;i++){
                            if(dataListCache[i].AutoId == dataCache.AutoId){
                                dataListCache.splice(i,1);
                                break;
                            }
                        }
                    }
                    dataListCache.push(dataCache);
                    $('#roleList').datagrid('loadData', dataListCache); 
                    $.messager.alert("信息","操作成功","info");
                    $('#editContent').window('close');
                }else{
                    $.messager.alert("错误",response.Message,"error");
                }
            })
            .error(function(){
                $.messager.alert("错误","系统内部错误","error");
            })
            .complete(function() { 
                hideMask();
            });
        }
        function showModules(){
            var selected = $('#roleList').datagrid('getSelected');
            if(selected==null){
                $.messager.alert("警告","请先选中一笔记录","info");
                return;
            }
            resetTable();
            $('#detailContent').window('open');
        }
        function addItem(){
            if(editing){
                $('#roleModuleList').datagrid('appendRow',{AutoId:0,Flag:0,RoleId:dataCache.AutoId,Exe:'true'});
            }else{
                $.messager.alert("消息","先按编辑键进入编辑状态","info");
            }
        }
        function editTable(){
            if(!editing){
                $('#roleModuleList').datagrid("enableCellEditing"); 
                editing=true;
                $('#detailAdd').linkbutton("enable"); 
                $('#detailDel').linkbutton("enable"); 
                $('#detailSave').linkbutton("enable"); 
                $('#detailCancel').linkbutton("enable"); 
                $('#detailEdit').linkbutton("disable"); 

            }else{
                $.messager.alert("消息","当前正在编辑中","info");
            }
        }
        function setCombo(index, row){ 
            if(editing){
                var edModule = $('#roleModuleList').datagrid('getEditor', {index:index,field:'ModuleId'});
                if(edModule!=null){
                    $(edModule.target).combobox('loadData',modules);
                }
                var edCmd = $('#roleModuleList').datagrid('getEditor', {index:index,field:'Action'});
                if(edCmd!=null){
                    var dataBinding = [];
                    for(var i=0;i<cmds.length;i++){
                        if(row['ModuleId'] == cmds[i].ModuleId){
                            dataBinding.push(cmds[i]);
                        }
                    }
                    $(edCmd.target).combobox('loadData',dataBinding);
                }
            }
        }
        function setRowIndex(index,row){
            rowEditing = index;
        }
        function endEditTable(){
            $('#roleModuleList').datagrid('endEdit',rowEditing);
            $('#roleModuleList').datagrid("disableCellEditing"); 
            rowEditing = -1;
            editing = false;
            $('#detailAdd').linkbutton("disable"); 
            $('#detailDel').linkbutton("disable"); 
            $('#detailSave').linkbutton("disable"); 
            $('#detailCancel').linkbutton("disable"); 
            $('#detailEdit').linkbutton("enable"); 
        }
        function resetTable(){
            listModuleForRole(dataCache.AutoId);
            endEditTable();
        }
        function submitDetail(){
            if(rowEditing!=-1){
                var valid = $('#roleModuleList').datagrid('validateRow',rowEditing);
                if(!valid){
                    $.messager.alert("错误","错误的数据格式","error");
                    return;
                }
                $('#roleModuleList').datagrid('endEdit',rowEditing);
                var data = $('#roleModuleList').datagrid('getData');
                for(var i =0;i<data.rows.length;i++){
                    if(data.rows[i].Exe=='true'){data.rows[i].Exe=true;}else{data.rows[i].Exe=false;}
                }
                $.post("../../api/role/setmodule",{MasterId:dataCache.AutoId,Data:data.rows},function(response){
                    if(response.Result==0){
                        dataCache.Modules = JSON.parse(response.Data);
                    }else{
                        $.messager.alert("错误",response.Message,"error");
                    }
                })
                .error(function(){
                    $.messager.alert("错误","系统内部错误","error");
                })
                .complete(function() { 
                    endEditTable();
                });
            }else{
                endEditTable();
            }
        }
    </script>
<head>
<body>
	<div id="toolbar" class="easyui-panel" style="padding:1px; " >
		<a href="#" class="easyui-linkbutton" data-options="onClick:createNewRecord,iconCls:'icon-edit',plain:true">新建</a>
		<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true">删除</a>
		<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true">搜索</a>
        <a href="#" class="easyui-linkbutton" data-options="onClick:showModules, iconCls:'icon-more',plain:true">权限</a>
		<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-ok',plain:true,disabled:true">审核</a>
		<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-back',plain:true,disabled:true">反审</a>
	</div>
    <div style="margin:2px"></div>
    <table id="roleList" class="easyui-datagrid" 
        data-options=" singleSelect:true,method:'get',onClickRow:loadDataCache,onDblClickRow:editRecord">
        <thead>
            <tr>
                <th data-options="field:'AutoId',hidden:true"></th>
                <th data-options="field:'Flag',hidden:true"></th>
                <th data-options="field:'Code',width:120,align:'right'">权限代号</th>
                <th data-options="field:'Name',width:180,align:'right'">权限名称</th>
                <th data-options="field:'Note',width:340,align:'right'">备注</th>
            </tr>
        </thead>
    </table>
    <div id="editContent" class="easyui-window" title="编辑框" data-options="modal:true,closed:true,iconCls:'icon-save'" style="width:1000px;height:500px;padding:10px;">
        <div class="easyui-panel" style="padding:1px;width:100%">
            <a href="#" class="easyui-linkbutton" data-options="onClick:submitChange,iconCls:'icon-save',plain:true">保存</a>
            <a href="#" class="easyui-linkbutton" data-options="onClick:initVal,iconCls:'icon-no',plain:true">取消</a>
            <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-ok',plain:true,disabled:true">审核</a>
            <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-back',plain:true,disabled:true">反审</a>
        </div>
        <div style="margin:2px"></div>
        <div id="editZone" class="easyui-panel" style="padding:10px;width:100%;height:400px">
            <table>
                <tr>
                    <td><input class="easyui-textbox" id="Code"  style="width:300px" data-options="label:'权限代号:',required:true"></td>
                    <td>&nbsp;</td>
                    <td><input class="easyui-textbox" id="Name"  style="width:300px" data-options="label:'权限名称:'"></td>
                </tr>
                <tr>
                    <td colspan="3"><input class="easyui-textbox" id="Note" style="width:600px" data-options="label:'备注:'"></td>
                </tr>
                <tr>
                    <td  colspan="3"><input class="easyui-combobox" id="SiteSelected"  style="width:600px" data-options="label:'所属组织:',multiple:true,valueField: 'AutoId',textField: 'Name',"></td>
                </tr>
                <tr>
                    <td colspan="3"><input class="easyui-combobox" id="UserSelected"  style="width:600px" data-options="label:'所属用户:',multiline:true,multiple:true,valueField: 'AutoId',textField: 'Name',"></td>
                </tr>
            </table>
            <div hidden>
                <p id="AutoId"></p>
                <p id="Flag"></p>
            </div>
        </div>
	</div>
    <div id="detailContent" class="easyui-window" title="明细框" data-options="modal:true,closed:true,iconCls:'icon-save'" style="width:1000px;height:500px;padding:10px;">
        <div class="easyui-panel" style="padding:1px;width:100%">
            <a href="#" id="detailEdit" class="easyui-linkbutton" data-options="onClick:editTable,iconCls:'icon-edit',plain:true">开始</a>
            <a href="#" id="detailAdd"  class="easyui-linkbutton" data-options="onClick:addItem,iconCls:'icon-add',plain:true,disabled:true">增行</a>
            <a href="#" id="detailDel"  class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true,disabled:true">删行</a>
            <a href="#" id="detailSave"  class="easyui-linkbutton" data-options="onClick:submitDetail,iconCls:'icon-save',plain:true,disabled:true">保存</a>
            <a href="#" id="detailCancel"   class="easyui-linkbutton" data-options="onClick:resetTable,iconCls:'icon-no',plain:true,disabled:true">取消</a>
        </div>
        <div style="margin:2px"></div>
        <table id="roleModuleList" class="easyui-datagrid" style="width:100%;" data-options="singleSelect:true,method:'get',height:'400',onBeginEdit:setCombo,onClickRow:setRowIndex">
            <thead>
                <tr>
                    <th data-options="field:'AutoId',hidden:true"></th>
                    <th data-options="field:'Flag',hidden:true"></th>
                    <th data-options="field:'RoleId',hidden:true"></th>
                    <th data-options="field:'ModuleId',width:180,align:'right',
                        formatter:function(value,row,index){
                            for(var i=0;i<modules.length;i++){
                                if(modules[i].AutoId==value){
                                    return modules[i].Name;
                                }
                            }
                        },
                        editor:{
                            type:'combobox',
                            options:{
                                valueField:'AutoId',
                                textField:'Name',
                            }
                        }">模块</th>
                    <th data-options="field:'Action',width:180,align:'right',
                        formatter:function(value,row,index){
                            if(cmds==null){
                                initCmdList();
                            }
                            for(var i=0;i<cmds.length;i++){
                                if(cmds[i].Code==value){
                                    return cmds[i].Name;
                                }
                            }
                        },
                        editor:{
                            type:'combobox',
                            options:{
                                valueField:'Code',
                                textField:'Name',
                            }
                        }">命令</th>
                    <th data-options="field:'Exe',width:100,align:'right', formatter:function(value,row,index){if(value=='true'){return '是';}else{return '否';}},editor:{type:'checkbox',options:{on:'true',off:'false'}}">执行</th>
                </tr>
            </thead>
        </table>
	</div>
</body>
</html>